package com.demo.java.OD551_600.OD583;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【贪吃蛇(B卷-100分)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146569539
 */
public class OdMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // 读取操作命令
        String operators = scanner.nextLine();

        // 读取网格大小
        String input_str = scanner.nextLine();
        String[] tmp2 = input_str.split(" ");
        int[] nums = new int[tmp2.length];
        for (int i = 0; i < tmp2.length; i++) {
            nums[i] = Integer.parseInt(tmp2[i]);
        }

        int row = nums[0];  // 行数
        int column = nums[1];  // 列数
        String[][] board = new String[row][column];  // 创建网格
        ArrayList<Path> snake = new ArrayList<>();  // 存储蛇的身体

        // 读取网格并初始化蛇的头部
        for (int i = 0; i < board.length; i++) {
            String input_str1 = scanner.nextLine();
            String[] tmp3 = input_str1.split(" ");
            for (int j = 0; j < board[0].length; j++) {
                if ("H".equals(tmp3[j])) {  // 找到蛇头的位置
                    snake.add(new Path(i, j, "L"));  // 初始方向设置为向左
                }
                board[i][j] = tmp3[j];  // 填充网格
            }
        }

        // 执行操作命令
        for (int i = 0; i < operators.length(); i++) {
            char operator = operators.charAt(i);
            if (operator == 'U') {
                snake.get(0).setDirection("U");  // 向上
            } else if (operator == 'D') {
                snake.get(0).setDirection("D");  // 向下
            } else if (operator == 'L') {
                snake.get(0).setDirection("L");  // 向左
            } else if (operator == 'R') {
                snake.get(0).setDirection("R");  // 向右
            } else { // G 移动
                move(board, snake);  // 执行一次移动
            }
        }
    }

    // 移动函数
    public static void move(String[][] board, ArrayList<Path> snake) {
        Path head = snake.get(0);  // 获取蛇头
        String direction = head.getDirection();
        int row = 0;
        int column = 0;

        // 根据方向更新蛇头位置
        if (direction.equals("U")) {
            row = head.getRow() - 1;
            column = head.getColumn();
        } else if (direction.equals("L")) {
            row = head.getRow();
            column = head.getColumn() - 1;
        } else if (direction.equals("R")) {
            row = head.getRow();
            column = head.getColumn() + 1;
        } else if (direction.equals("D")) {
            row = head.getRow() + 1;
            column = head.getColumn();
        }

        // 判断是否越界
        if (row >= board.length || column >= board[0].length || row < 0 || column < 0) {
            System.out.println(snake.size());  // 越界，游戏结束
            return;
        }

        // 判断是否碰到自己
        for (int i = 1; i < snake.size() - 1; i++) {
            if (row == snake.get(i).getRow() && column == snake.get(i).getColumn()) {
                System.out.println(snake.size());  // 碰到自己，游戏结束
                return;
            }
        }

        // 判断是否碰到食物
        if (board[row][column].equals("F")) {
            snake.add(0, new Path(row, column, direction));  // 吃到食物，蛇身增长
            return;
        }

        // 正常移动
        snake.add(0, new Path(row, column, direction));
        snake.remove(snake.size() - 1);  // 移除尾部
    }
}

// 蛇身的每一部分的路径类
class Path {
    int row;
    int column;
    String direction;

    public Path(int row, int column, String direction) {
        this.row = row;
        this.column = column;
        this.direction = direction;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }
}
